﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EyeballLib;
using System.Threading.Tasks;

namespace EyeballFetcher
{
    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<SvnCrawler> Crawlers { get; set; }
        public List<Task> Tasks { get; set; }

        public delegate void UpdateProgress();

        public UpdateProgress UpdateProgressSafe;


        public MainWindow()
        {
            Crawlers = new List<SvnCrawler>();
            Tasks = new List<Task>();
            InitializeComponent();
            UpdateProgressSafe = new UpdateProgress(listView1.Items.Refresh);
            listView1.ItemsSource = Crawlers;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxName.Text) && string.IsNullOrEmpty(textBoxLL.Text) && string.IsNullOrEmpty(textBoxRL.Text))
                return;

            var repository = new Repository
                                        {
                                            Name = textBoxName.Text,
                                            RemoteLocation = textBoxRL.Text,
                                            LocalLocation = textBoxLL.Text,
                                            Type = comboBox1.Text,
                                            ExtensionsAllowed = checkBox1.IsChecked ?? false
                                        };

            foreach (var item in listBox1.Items)
            {
                repository.Extensions.Add(new Extension{ Extension1 = item.ToString(), Repository = repository.Name, Repository1 = repository});
            }

            var crawler = new SvnCrawler(repository);
            Crawlers.Add(crawler);
            crawler.CommitFinished += crawler_CommitFinished;
            var task = Task.Factory.StartNew(crawler.FetchCommits);
            Tasks.Add(task);
            task.ContinueWith((t) => RefreshList());
            listView1.Items.Refresh();
        }

        void crawler_CommitFinished(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void RefreshList()
        {
            if (!listView1.Dispatcher.CheckAccess())
            {
                listView1.Dispatcher.Invoke(UpdateProgressSafe);
            }
            else
            {
                listView1.Items.Refresh();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            foreach (var task in Tasks)
                task.Dispose();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                if (e.Key == Key.Enter)
                {
                    listBox1.Items.Add(textBox1.Text);
                    textBox1.Clear();
                }
            }
        }
    }
}
